﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using RJ.Objects.GameEngine;
using System.Windows.Forms;
using RJ.Objects.Workers;
using RJ.Euchre.App.Game;
using RJ.Objects.PokerCards;
using RJ.Objects.GameRules;

namespace RJ.Euchre.App.Views
{
    public class EuchreTableView : GameView
    {
        public const int SPRITE_WIDTH = 90;
        public const int SPRITE_HEIGHT = 126;

        private Point position;
        private int counter;
        private SpriteSheet cardFace;
        private SpriteSheet cardBack;
        private Background background;
        private Point pointOnSprite;
        private Size sizeOfSprite;
        private Random rand;
        private Dictionary<string, Point> cardSpritePoints;
        private int currentPlayer;
        public List<EuchrePlayer> players;
        private bool needsToBeDealt;

        public EuchreDeck deck;
        public EuchreSettings es;
        public EuchreRules rules;

        public event NotifyTurn TurnNotification;

        public EuchreTableView(GameObject game)
            : base(game as GameState)
        {
            position = new Point(0, 0);
            cardFace = new SpriteSheet(@"Resources\CardFace_sm.png");
            cardBack = new SpriteSheet(@"Resources\CardBacks_sm.png");
            background = new Background(game, @"Resources\Background.png");
            GameObjects = new List<GameObject>();
            sizeOfSprite = new Size(SPRITE_WIDTH, SPRITE_HEIGHT);
            rand = new Random(Environment.TickCount);
            es = ((game as GameState).settings as EuchreSettings);
            rules = new EuchreRules();

            cardSpritePoints = (new JsonFileReaderWriter<Dictionary<string, Point>>(@"Resources\deckPoints_sm.json")).Read();
        }

        public override void Initialize()
        {
            GameObjects.Clear();
            deck = new EuchreDeck(gameState, cardFace, cardSpritePoints, true, cardBack, (es != null ? es.CardBack : 1));
            needsToBeDealt = true;
            pointOnSprite = new Point(0);
            createPlayers();
            GameObjects.Add(deck);
        }

        private void createPlayers()
        {
            PlayerType playerType = PlayerType.Human;
            EuchrePlayingState state = EuchrePlayingState.Waiting;
            int pseudoRandomDealer = rand.Next(1, 4);
            players = new List<EuchrePlayer>();

            for (int i = 0; i < 4; i++)
            {
                if (i + 1 == pseudoRandomDealer)
                    state = EuchrePlayingState.Dealing;

                var player = new EuchrePlayer(gameState, playerType, AI.Dumb, null, i + 1);
                player.PlayingState = state;
                player.TurnFinishedNotification += new NotifyTurn(player_TurnFinishedNotification);
                TurnNotification += new NotifyTurn(player.TurnNotification);
                players.Add(player);
                GameObjects.Add(player);
                playerType = PlayerType.Computer;
                state = EuchrePlayingState.Waiting;
            }
        }

        public override void Update(double gameTime, double elapsedTime)
        {
            // consume a mouseevent first
            if (!_isPaused)
            {
                ConsumeMouseEvent();

                // updates any gameObjects that are registered in GameObjects
                base.Update(gameTime, elapsedTime);
            }
        }

        public override void Draw(Graphics g)
        {
            background.Draw(g);
            // the base Draw runs through the list of GameObjects to draw them
            base.Draw(g);

            counter++;
        }

        public override void ConsumeMouseEvent()
        {
            var mouseEvent = gameState.GetMouseEvent();
            if (mouseEvent != null)
            {
                GameObjects.Add(new Sprite(this, cardFace, new Rectangle(pointOnSprite, sizeOfSprite), new RectangleF(mouseEvent.Location, sizeOfSprite), new PointF(rand.Next(20, 50), 0f)));

                pointOnSprite.X += sizeOfSprite.Width;
                if (pointOnSprite.X >= sizeOfSprite.Width * 13)
                {
                    pointOnSprite.X = 0;
                    pointOnSprite.Y += sizeOfSprite.Height;
                }

                if (pointOnSprite.Y >= sizeOfSprite.Height * 4)
                {
                    pointOnSprite.Y = 0;
                }
            }
        }

        public override void ConsumeKeyboardEvent()
        {
            throw new NotImplementedException();
        }

        private void OnNotifyTurn()
        {
            if (TurnNotification != null)
                TurnNotification(this, new EuchreGameEventArgs(gameState, EventState.NotifyTurn, EuchrePlayingState.Playing, currentPlayer));
        }

        private void player_TurnFinishedNotification(object sender, GameEventArgs gameEventArgs)
        {
            var egea = gameEventArgs as EuchreGameEventArgs;
            if (egea != null)
            {
                Console.WriteLine("{0} Player {1} has finished their turn", (sender as EuchrePlayer).PlayerType, (sender as EuchrePlayer).Player);
            }
        }
    }
}
